package com.lanvideo.task;

import com.lanvideo.dao.QuestionDao;
import com.lanvideo.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class QuestionInsertTask {

    @Autowired
    private QuestionDao questionDao;

    @PostConstruct
    public void insertQuestions() {
        try {
            // 检查是否已经有题目了
            if (questionDao.getTotalCount() > 0) {
                System.out.println("数据库中已有题目，跳过插入");
                return;
            }

            List<Question> questions = createQuestions();
            System.out.println("准备插入 " + questions.size() + " 个题目");
            
            int successCount = 0;
            for (Question question : questions) {
                try {
                    questionDao.save(question);
                    successCount++;
                    System.out.println("成功插入题目: " + question.getContent());
                } catch (Exception e) {
                    System.err.println("插入题目失败: " + question.getContent() + ", 错误: " + e.getMessage());
                }
            }
            
            System.out.println("成功插入 " + successCount + " 个题目，总共 " + questions.size() + " 个题目");
        } catch (Exception e) {
            System.err.println("插入题目失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private List<Question> createQuestions() {
        List<Question> questions = new ArrayList<>();

        // 单选题 (type=1)
        questions.add(new Question("Java中哪个关键字用于继承？", "extends", "implements", "inherit", "super", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中String类是什么类型？", "基本类型", "引用类型", "数组类型", "接口类型", "B", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于创建对象？", "new", "create", "make", "build", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中int类型的默认值是什么？", "0", "null", "undefined", "1", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于定义常量？", "final", "const", "static", "constant", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于包声明？", "package", "import", "class", "interface", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于接口声明？", "interface", "class", "abstract", "extends", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于抽象类？", "abstract", "interface", "final", "static", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于静态方法？", "static", "final", "abstract", "private", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于私有访问？", "private", "public", "protected", "default", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于公共访问？", "public", "private", "protected", "default", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于受保护访问？", "protected", "public", "private", "default", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于方法重写？", "override", "overload", "extends", "implements", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于方法重载？", "overload", "override", "extends", "implements", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于异常处理？", "try", "catch", "finally", "throw", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于捕获异常？", "catch", "try", "finally", "throw", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于抛出异常？", "throw", "throws", "try", "catch", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于声明异常？", "throws", "throw", "try", "catch", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于同步？", "synchronized", "volatile", "transient", "native", "A", 1, 1, "Java基础", 1));
        questions.add(new Question("Java中哪个关键字用于易变变量？", "volatile", "synchronized", "transient", "native", "A", 1, 1, "Java基础", 1));

        // 多选题 (type=2)
        questions.add(new Question("Java中的访问修饰符有哪些？", "public", "private", "protected", "default", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的数据类型有哪些？", "基本类型", "引用类型", "数组类型", "接口类型", "AB", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的循环语句有哪些？", "for", "while", "do-while", "foreach", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的条件语句有哪些？", "if", "else", "switch", "case", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的异常类型有哪些？", "RuntimeException", "CheckedException", "Error", "Exception", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的集合框架有哪些？", "List", "Set", "Map", "Queue", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的线程状态有哪些？", "NEW", "RUNNABLE", "BLOCKED", "WAITING", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的内存区域有哪些？", "堆内存", "栈内存", "方法区", "程序计数器", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的设计模式有哪些？", "单例模式", "工厂模式", "观察者模式", "装饰器模式", "ABCD", 2, 2, "Java基础", 1));
        questions.add(new Question("Java中的注解有哪些？", "@Override", "@Deprecated", "@SuppressWarnings", "@SafeVarargs", "ABCD", 2, 2, "Java基础", 1));

        // 判断题 (type=3)
        questions.add(new Question("Java是一种编译型语言", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java是一种解释型语言", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java支持多重继承", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java支持单继承", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的String是不可变的", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的String是可变的", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的数组是对象", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的数组不是对象", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的接口可以有方法实现", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的接口不能有方法实现", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的抽象类可以有构造函数", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的抽象类不能有构造函数", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的静态方法可以被重写", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的静态方法不能被重写", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的final变量可以被修改", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的final变量不能被修改", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的transient变量会被序列化", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的transient变量不会被序列化", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的volatile变量保证可见性", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的volatile变量不保证可见性", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的synchronized关键字用于同步", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的synchronized关键字不用于同步", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的native方法用Java编写", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的native方法不用Java编写", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的strictfp关键字用于浮点运算", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的strictfp关键字不用于浮点运算", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的assert关键字用于断言", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的assert关键字不用于断言", "", "", "", "", "F", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的enum是关键字", "", "", "", "", "T", 3, 1, "Java基础", 1));
        questions.add(new Question("Java中的enum不是关键字", "", "", "", "", "F", 3, 1, "Java基础", 1));

        return questions;
    }
}
